/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2010
 */

package com.hibernate.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.javacommon.base.BaseEntity;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import cn.org.rapid_framework.util.DateConvertUtils;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Entity
@Table(name = "LABEL")
public class Label extends BaseEntity implements java.io.Serializable {
	private static final long serialVersionUID = 5454155825314635342L;

	// alias
	public static final String TABLE_ALIAS = "网站栏目";
	public static final String ALIAS_ID = "id";
	public static final String ALIAS_TYPE_ID = "栏目类型ID";
	public static final String ALIAS_WEBSITE_ID = "所属网站ID";
	public static final String ALIAS_NAME = "栏目名称";
	public static final String ALIAS_AD_SITE_AMOUNT = "广告位数量";
	public static final String ALIAS_CREATION_TIME = "添加时间";
	public static final String ALIAS_STATUS = "状态";

	// date formats
	public static final String FORMAT_CREATION_TIME = DATE_FORMAT;
	

	// 可以直接使用: @Length(max=50,message="用户名长度不能大于50")显示错误消息
	// columns START

	private Long id;
	@NotNull
	private Long typeId;
	@NotNull
	private Long websiteId;
	@Length(max = 255)
	private String otherCode;
	@NotBlank
	@Length(max = 50)
	private java.lang.String name;
	@NotNull
	private Long adSiteAmount;
	@NotNull
	private java.util.Date creationTime;
	@NotBlank
	@Length(max = 1)
	private java.lang.String status;

	private Set<Long> idSet;

	// columns END

	public Label() {
	}

	public Label(Long id) {
		this.id = id;
	}

	public void setIdSet(Set<Long> idSet) {
		this.idSet = idSet;
	}

	@Transient
	public Set<Long> getIdSet() {
		return idSet;
	}

	public void setId(Long value) {
		this.id = value;
	}

	@Id
	@GeneratedValue(generator = "paymentableGenerator")
	@GenericGenerator(name = "paymentableGenerator", strategy = "native")
	@Column(name = "ID", unique = true, nullable = false, insertable = true, updatable = true, length = 20)
	public Long getId() {
		return this.id;
	}

	@Column(name = "TYPE_ID", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	public Long getTypeId() {
		return this.typeId;
	}

	public void setTypeId(Long value) {
		this.typeId = value;
	}

	@Column(name = "WEBSITE_ID", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	public Long getWebsiteId() {
		return this.websiteId;
	}

	public void setWebsiteId(Long value) {
		this.websiteId = value;
	}

	@Column(name = "NAME", unique = false, nullable = false, insertable = true, updatable = true, length = 50)
	public java.lang.String getName() {
		return this.name;
	}

	public void setName(java.lang.String value) {
		this.name = value;
	}

	@Column(name = "AD_SITE_AMOUNT", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	public Long getAdSiteAmount() {
		return this.adSiteAmount;
	}

	public void setAdSiteAmount(Long value) {
		this.adSiteAmount = value;
	}

	@Transient
	public String getCreationTimeString() {
		return DateConvertUtils.format(getCreationTime(), FORMAT_CREATION_TIME);
	}

	public void setCreationTimeString(String value) {
		setCreationTime(DateConvertUtils.parse(value, FORMAT_CREATION_TIME,
				java.util.Date.class));
	}

	@Column(name = "CREATION_TIME", unique = false, nullable = false, insertable = true, updatable = true, length = 7)
	public java.util.Date getCreationTime() {
		return this.creationTime;
	}

	public void setCreationTime(java.util.Date value) {
		this.creationTime = value;
	}

	@Column(name = "STATUS", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
	public java.lang.String getStatus() {
		return this.status;
	}

	public void setStatus(java.lang.String value) {
		this.status = value;
	}

	private Set adSites = new HashSet(0);

	public void setAdSites(Set<AdSite> adSite) {
		this.adSites = adSite;
	}

	@OneToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "label")
	@OrderBy("name")
	public Set<AdSite> getAdSites() {
		return adSites;
	}

	private Website website;

	public void setWebsite(Website website){

		this.website = website;
	}

	@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
	@JoinColumns({ @JoinColumn(name = "WEBSITE_ID", nullable = false, insertable = false, updatable = false) })
	public Website getWebsite() {
		return website;
	}

	private LabelType labelType;

	public void setLabelType(LabelType labelType) {
		if (labelType != null)
			labelType.getName();
		this.labelType = labelType;
	}

	@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
	@JoinColumns({ @JoinColumn(name = "TYPE_ID", nullable = false, insertable = false, updatable = false) })
	public LabelType getLabelType() {
		return labelType;
	}

	@Column(name = "OTHER_CODE", unique = false, nullable = true, insertable = true, updatable = true, length = 255)
	public String getOtherCode() {
		return this.otherCode;
	}

	public void setOtherCode(String value) {
		this.otherCode = value;
	}

	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
				.append("Id", getId()).append("TypeId", getTypeId())
				.append("WebsiteId", getWebsiteId()).append("Name", getName())
				.append("AdSiteAmount", getAdSiteAmount())
				.append("CreationTime", getCreationTime())
				.append("Status", getStatus()).toString();
	}

	public int hashCode() {
		return new HashCodeBuilder().append(getId()).toHashCode();
	}

	public boolean equals(Object obj) {
		if (obj instanceof Label == false)
			return false;
		if (this == obj)
			return true;
		Label other = (Label) obj;
		return new EqualsBuilder().append(getId(), other.getId()).isEquals();
	}

	@Transient
	@Override
	public String getTarget() {
		// TODO Auto-generated method stub
		return "媒体栏目";
	}

	@Transient
	@Override
	public Long getTargetId() {
		// TODO Auto-generated method stub
		return getId();
	}

	@Transient
	@Override
	public String getTargetName() {
		// TODO Auto-generated method stub
		return getName();
	}
}
